package com.pbd.server;

import java.io.File;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
//import com.pbd.shared.SessionInfo;
import com.pbd.client.UserLoginService;

public class UserLoginServiceImpl extends RemoteServiceServlet implements UserLoginService {

	public List checkLogin(String userName, String password) {
		return null;// checkLogin(userName, password, null);
	}

	// private List checkLogin(String userName, String password, String partyID)
	// {
	//
	// PARTYLOGIN partyLoginBdo = new PARTYLOGIN();
	// List resultMap = new ArrayList();
	// HibernateServiceImpl hibernateServiceImpl = new HibernateServiceImpl();
	// String loginUserName = null;
	// if (partyID == null) {
	// List specialUserList = hibernateServiceImpl.findObjects("PARTYLOGIN", new
	// String[] { "loginUserName" }, "loginUserName='" + userName +
	// "' AND isSuperUser=1");
	// int specialUserSize = specialUserList == null ? 0 :
	// specialUserList.size();
	//
	// if (specialUserSize == 1) {
	// List afcp_UserList = hibernateServiceImpl.findObjects("PARTYLOGIN", new
	// String[] { "loginUserName", "party.partyID", "party.partyName" },
	// "PARTYLOGIN.loginUserName='" + userName
	// + "' AND PARTYLOGIN.loginPassword='" + password + "'");
	// int afcp_UserSize = afcp_UserList == null ? 0 : afcp_UserList.size();
	// if (afcp_UserSize == 1) {
	// ArrayList dataValueList = (ArrayList) afcp_UserList.get(0);
	//
	// if (dataValueList != null && dataValueList.size() > 0) {
	// loginUserName = (String) dataValueList.get(0);
	// Long loginPartyID = (Long) dataValueList.get(1);
	// String loginPartyName = (String) dataValueList.get(2);
	// PARTY party = new PARTY();
	// party.setPartyID(loginPartyID);
	// party.setPartyName(loginPartyName);
	//
	// resultMap.add(populateUserInSession(party, loginUserName));
	// return resultMap;
	// }
	// }
	// } else {
	//
	// Long partyId = null;
	// Long chapterId = null;
	// String roleName = null;
	// List registeredUserList = hibernateServiceImpl.findObjects("PARTYLOGIN",
	// new String[] { "party.partyID", "party.partyName" },
	// " PARTYLOGIN.loginUserName='" + userName
	// + "' AND (PARTYLOGIN.loginPassword='" + password +
	// "') AND PARTYLOGIN.isSuperUser=0");
	// int registeredUserSize = registeredUserList == null ? 0 :
	// registeredUserList.size();
	// if (registeredUserSize == 1) {
	// List partyLoginDataList = (ArrayList) registeredUserList.get(0);
	// loginUserName = userName;
	//
	// PARTY party = new PARTY();
	// if (partyLoginDataList.get(0) != null) {
	// partyId = (Long) partyLoginDataList.get(0);
	// party.setPartyID((Long) partyLoginDataList.get(0));
	// }
	// if (partyLoginDataList.get(1) != null)
	// party.setPartyName((String) partyLoginDataList.get(1));
	//
	// if (partyLoginDataList != null) {
	// resultMap.add(populateUserInSession(party, userName));
	// return resultMap;
	// }
	// }
	// }
	// }
	// resultMap.add("Authentication Failed");
	// return resultMap;
	// }
	//
	private String encrypt(String str) throws SecurityException {
		String encryptValue;
		try {
			CryptoLibrary crpto = new CryptoLibrary();
			encryptValue = crpto.encrypt(str);
			return encryptValue;
		} catch (Exception e) {
			throw new SecurityException("Could not encrypt: " + e.getMessage());
		}
	}

	// private Object populateUserInSession(PARTY party, String userName) {
	//
	// HibernateServiceImpl hibernateServiceImpl = new HibernateServiceImpl();
	// String ipAddress = "No IP";
	// if (hibernateServiceImpl.WEB_MODE) {
	// ipAddress = getThreadLocalRequest().getRemoteAddr();
	// } else {
	// try {
	// java.net.InetAddress inetAddress = java.net.InetAddress.getLocalHost();
	// ipAddress = inetAddress.getHostAddress();
	// } catch (UnknownHostException e) {
	// e.printStackTrace();
	// }
	// }
	//
	// System.err.println("IP Address :: " + ipAddress);
	//
	// HttpSession httpSession = getThreadLocalRequest().getSession(true);
	// String JSessionID = httpSession.getId();
	// SessionInfo userSessionInfo = SessionInfo.getSharedInstance();
	// userSessionInfo.clearSessionMap();
	// if (party != null) { // For Normal User
	//
	// Long partyId = null;
	// String roleName = null;
	// String partyName = null;
	// String chapterName = null;
	// Long chapterId = null;
	// List chapterPartyRelationList =
	// hibernateServiceImpl.findObjects("CHAPTERPARTYRELATION", new String[] {
	// "party.partyID", "party.partyName", "chapter.chapterID",
	// "chapter.chapterName",
	// "roleName" }, "CHAPTERPARTYRELATION.party.partyID =" + party.getPartyID()
	// + " AND CHAPTERPARTYRELATION.isActive=1");
	// int chapterRelatioSize = chapterPartyRelationList == null ? 0 :
	// chapterPartyRelationList.size();
	// if (chapterRelatioSize == 1) {
	// List chapterList = (ArrayList) chapterPartyRelationList.get(0);
	// if (chapterList.get(0) != null) {
	// partyId = (Long) chapterList.get(0);
	// partyName = (String) chapterList.get(1);
	// chapterId = (Long) chapterList.get(2);
	// chapterName = (String) chapterList.get(3);
	// roleName = (String) chapterList.get(4);
	//
	// userSessionInfo.putSessionData(SessionInfo.SESSION_PARTYID,
	// String.valueOf(partyId));
	// userSessionInfo.putSessionData(SessionInfo.SESSION_PARTYNAME, partyName);
	// if (chapterId != null)
	// userSessionInfo.putSessionData(SessionInfo.SESSION_CHAPTER_ID,
	// String.valueOf(chapterId));
	// userSessionInfo.putSessionData(SessionInfo.SESSION_CHAPTERNAME,
	// chapterName);
	// userSessionInfo.putSessionData(SessionInfo.SESSION_ROLENAME, roleName);
	// userSessionInfo.putSessionData(SessionInfo.SESSION_USERNAME, userName);
	//
	// }
	// }
	// }
	// userSessionInfo.putSessionData(SessionInfo.SESSION_EMAIL, userName);
	// userSessionInfo.putSessionData(SessionInfo.JSESSION_ID, JSessionID);
	// userSessionInfo.putSessionData(SessionInfo.SESSION_IP_ADDRESS,
	// ipAddress);
	// httpSession.setMaxInactiveInterval((int) 1800);
	// System.err.println("userSessionInfo : " + userSessionInfo.toString());
	// httpSession.setAttribute(httpSession.getId(), userSessionInfo);
	// return userSessionInfo;
	// }

	public synchronized String decrypt(String str) throws SecurityException {
		String decryptValue = null;
		try {

			CryptoLibrary crpto = new CryptoLibrary();
			if (str != null && !str.equalsIgnoreCase("")) {
				decryptValue = crpto.decrypt(str);
				return decryptValue;
			} else
				return "";
		} catch (Exception e) {
			throw new SecurityException("Could not decrypt: " + e.getMessage());
		}
	}

	public String logout() {
		HttpSession session = getThreadLocalRequest().getSession(false);
		// SessionInfo userSessionInfo = SessionInfo.getSharedInstance();
		// HibernateServiceImpl hibernateService = new HibernateServiceImpl();
		String sessionID = null;
		try {
			// if (session != null) {
			// String remoteIPAddress = getThreadLocalRequest().getRemoteAddr();
			// System.err.println("userSessionInfo before =[" + userSessionInfo
			// + "]");
			// sessionID = session.getId();
			//
			// Map<String, Object> dataMap = new HashMap();
			// dataMap.put("exitDate", new Date());
			// hibernateService.updateObject("CHAPTERVISITORRELATION", dataMap,
			// "sessionId='" + sessionID +"'");
			//
			// if (getThreadLocalRequest().isRequestedSessionIdValid()) {
			// System.out.println("Remove :: " +
			// userSessionInfo.getSessionData(SessionInfo.SESSION_EMAIL) + ";" +
			// remoteIPAddress);
			// session.removeAttribute(sessionID);
			// session.invalidate();
			// userSessionInfo.clearSessionMap();
			// SessionInfo.setUserSessionInfo(null);
			// }
			// }
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		// System.err.println("userSessionInfo after =[" + userSessionInfo +
		// "]");
		return sessionID;
	}

	public Map getImages(String directoryName) {

		// String location =
		// getThreadLocalRequest().getClass().getProtectionDomain().getClassLoader().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

		String realPath = getThreadLocalRequest().getSession().getServletContext().getRealPath(directoryName);
		// String path= location.substring(0, location.indexOf("/tomcat")) +
		// directoryName;
		File dir = new File(realPath);
		Map filePathList = new HashMap();
		String[] children = dir.list();
		if (children == null) {
			// Either dir does not exist or is not a directory
		} else {
			for (int i = 0; i < children.length; i++) {
				// Get filename of file or directory
				String filename = children[i];
				System.err.println("." + directoryName + "/" + filename);
				filePathList.put(i + 1, "." + directoryName + "/" + filename);
			}
		}

		return filePathList;
	}

	
}
